package com.example.electronicmall.base;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.electronicmall.R;
import com.example.electronicmall.fragment.orderFagment.MineAllOrderFragment;
import com.example.electronicmall.fragment.orderFagment.MineDeliverFragment;
import com.example.electronicmall.fragment.orderFagment.MineEvaluateFragment;
import com.example.electronicmall.fragment.orderFagment.MinePaymentFragment;
import com.example.electronicmall.fragment.orderFagment.MineTakeFragment;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class OrderActivity extends AppCompatActivity {
    private ArrayList<String> mTabList = new ArrayList<>(
            Arrays.asList("全部", "待付款", "待发货", "待收货", "待评价"));
    List<Fragment> fragmentList;
    int order = 0;
    Bundle bundle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order2);

        TabLayout tabL = findViewById(R.id.tabL);
        ViewPager viewPager = findViewById(R.id.viewpager);
        fragmentList = new ArrayList<>();
        fragmentList.add(new MineAllOrderFragment());
        fragmentList.add(new MinePaymentFragment());
        fragmentList.add(new MineDeliverFragment());
        fragmentList.add(new MineTakeFragment());
        fragmentList.add(new MineEvaluateFragment());
        HomeAdapter homeAdapter = new HomeAdapter(getSupportFragmentManager(), fragmentList);
        //设置适配器到viewPager
        viewPager.setAdapter(homeAdapter);
        tabL.setupWithViewPager(viewPager);

        //设置到指定fragment页面
        bundle = getIntent().getExtras();

        if (bundle != null) {
            order = bundle.getInt("order");
            viewPager.setCurrentItem(order);
        }

        TextView orderFinish = findViewById(R.id.orderFinish);
        orderFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    //创建适配器类
    class HomeAdapter extends FragmentPagerAdapter {
        // 定义Fragment列表来存放Fragment
        List<Fragment> fragmentList;

        public HomeAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            this.fragmentList = fragments;
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        //用来显示标题
        @Override
        public CharSequence getPageTitle(int position) {
            return mTabList.get(position);
        }

    }
}